package com.ssm.web.action;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ssm.bean.Item;
import com.ssm.bean.ItemCheck;
import com.ssm.bean.Teacher;
import com.ssm.util.Constant;
import com.ssm.util.FieldUtil;
import com.ssm.util.MapUtil;
import com.ssm.util.MessageUtil;
import com.ssm.util.Page;
import com.ssm.util.common.BaseAction;
import com.ssm.web.service.ItemCheckService;
import com.ssm.web.service.ItemService;
import com.ssm.web.service.TeacherService;

@Controller
@RequestMapping("/sys")
public class ItemCheckAction extends BaseAction {
	@Autowired
	private TeacherService		teacherService;
	@Autowired
	private ItemService				itemService;
	private String						actionname	= "评价";
	private String						actionclass	= "ItemCheck";
	@Autowired
	private ItemCheckService	service;

	@RequestMapping(value = "/add2ItemCheck.do")
	public String add2() {
		request.setAttribute("actionname", actionname);
		request.setAttribute("actionclass", actionclass);
		return "addItemCheck";
	}

	@RequestMapping(value = "/getItemCheckStudent.do")
	public String getItemCheckStudent(int uid) {
		try {
			Teacher teacher = teacherService.get(uid);
			putRequestValue("teacher", teacher);

			List<Item> itemList = itemService.selectAll(Item.class, MapUtil.createMap("typesel", "学生评教"));
			putRequestValue("itemList", itemList);

			ItemCheck selectOne = service.selectOne(ItemCheck.class,
					MapUtil.createMap("teacherId", uid, "typesel", "学生评教", "userId", getSimpleUser().getId()));
			putRequestValue("result", selectOne);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "getItemCheckStudent";
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "获取信息失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/getItemCheckTeacher.do")
	public String getItemCheckTeacher(int uid) {
		try {
			Teacher teacher = teacherService.get(uid);
			putRequestValue("teacher", teacher);

			List<Item> itemList = itemService.selectAll(Item.class, MapUtil.createMap("typesel", "教师互评"));
			putRequestValue("itemList", itemList);

			ItemCheck selectOne = service.selectOne(ItemCheck.class,
					MapUtil.createMap("teacherId", uid, "typesel", "教师互评", "userId", getTeacher().getId()));
			putRequestValue("result", selectOne);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "getItemCheckTeacher";
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "获取信息失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/updateItemCheckStudent.do")
	public String updateItemCheckStudent(ItemCheck bean) {
		try {
			Teacher teacher = teacherService.get(bean.getTeacherId());
			JSONArray ary = new JSONArray();
			List<Item> itemList = itemService.selectAll(Item.class, MapUtil.createMap("typesel", "学生评教"));
			int score = 0;
			for (Item item : itemList) {
				int tempscore = getInt("score_" + item.getId());
				score += tempscore;
				JSONObject obj = new JSONObject();
				obj.put("id", item.getId());
				obj.put("score", tempscore);
				ary.add(obj);
			}

			ItemCheck c = new ItemCheck();
			c.setCresult(ary.toJSONString());
			c.setTeacherId(teacher.getId());
			c.setTypesel("学生评教");
			c.setUserId(getSimpleUser().getId());
			c.setFuser(getSimpleUser().getFname());
			c.setScore(score);
			c.setTeachername(teacher.getFname());
			service.add(c);

			MessageUtil.addMessage(request, "评价成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "评价失败.");
			return ERROR;
		}

	}

	@RequestMapping(value = "/updateItemCheckTeacher.do")
	public String updateItemCheckTeacher(ItemCheck bean) {
		try {
			Teacher teacher = teacherService.get(bean.getTeacherId());
			JSONArray ary = new JSONArray();
			List<Item> itemList = itemService.selectAll(Item.class, MapUtil.createMap("typesel", "教师互评"));
			int score = 0;
			for (Item item : itemList) {
				int tempscore = getInt("score_" + item.getId());
				score += tempscore;
				JSONObject obj = new JSONObject();
				obj.put("id", item.getId());
				obj.put("score", tempscore);
				ary.add(obj);
			}

			ItemCheck c = new ItemCheck();
			c.setCresult(ary.toJSONString());
			c.setTeacherId(teacher.getId());
			c.setTypesel("教师互评");
			c.setUserId(getTeacher().getId());
			c.setFuser(getTeacher().getFname());
			c.setScore(score);
			c.setTeachername(teacher.getFname());
			service.add(c);

			MessageUtil.addMessage(request, "评价成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "评价失败.");
			return ERROR;
		}

	}

	@RequestMapping(value = "/updateItemCheck.do")
	public String update(ItemCheck bean) {
		try {
			service.update(bean);
			MessageUtil.addMessage(request, "更新成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "更新失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/deleteItemCheck.do")
	public String delete(String ids) {
		try {
			service.deleteAll(ids);
			MessageUtil.addRelMessage(request, "操作成功.", "mainquery");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "操作失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/queryItemCheck.do")
	public String query() {
		try {
			// 字段名称集合
			LinkedList<String> parmnames = new LinkedList<String>();
			// 字段值集合
			LinkedList<Object> parmvalues = new LinkedList<Object>();
			Page p = FieldUtil.createPage(request, ItemCheck.class, parmnames, parmvalues);

			Page page = service.selectPage(p, ItemCheck.class);
			session.setAttribute(Constant.SESSION_PAGE, page);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "listItemCheck";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

}
